package com.dev.op.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dev.op.VO.ResultVO;
import com.dev.op.entity.TestCase;
import com.dev.op.service.TestCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/test-cases")
@PreAuthorize("hasAnyRole('admin','teacher','student')")
public class TestCaseController {

    @Autowired
    TestCaseService testCaseService;

    @GetMapping("/sample/problem/{problem-id}")
    public ResultVO<List<TestCase>> getSampleTestCases(@PathVariable("problem-id") String problemId) {
        LambdaQueryWrapper<TestCase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TestCase::getProblemId, problemId).eq(TestCase::getIsSample,1);
        return ResultVO.success(testCaseService.list(queryWrapper));
    }

    @PreAuthorize("hasRole('admin')")
    @GetMapping("/problem/{problem-id}")
    public ResultVO<List<TestCase>> getTestCases(@PathVariable("problem-id") String problemId) {
        LambdaQueryWrapper<TestCase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TestCase::getProblemId, problemId);

        List<TestCase> testCases =  testCaseService.list(queryWrapper);
        //System.out.println(testCases);
        return ResultVO.success(testCaseService.list(queryWrapper));
    }

    @PreAuthorize("hasRole('admin')")
    @PostMapping("/problem/{problem-id}")
    public ResultVO<Boolean> saveTestCases(@RequestBody List<TestCase> testCases,@PathVariable("problem-id") String problemId) {
        for(TestCase testCase : testCases) {
            testCase.setProblemId(Integer.parseInt(problemId));
        }
        testCaseService.saveBatch(testCases);
        return ResultVO.success(true);
    }


}
